package Controllers

import (
	"context"
	"encoding/json"
	"errors"
	"work1/DataBase"
	Model "work1/Models"
	service "work1/Service"
)

type RemoteController struct {
	commonController
}

func getIsp(AccountId any) (service.RemoteInterface, error) {
	var (
		ctx         context.Context
		AccountInfo map[string]any
		config      map[string]string
	)
	SessionModel := DataBase.DB.WithContext(ctx)
	SessionModel.Model(Model.Account{}).Where("id = ?", AccountId).Find(&AccountInfo)
	if len(AccountInfo) <= 0 {
		return nil, errors.New("账号不存在")
	}

	if err := json.Unmarshal([]byte(AccountInfo["config"].(string)), &config); err != nil {
		return nil, errors.New("账号配置有误" + err.Error())
	}
	var p service.RemoteInterface
	Baseconfig := &service.BaseConfig{AccessKeyId: config["accessKeyId"], AccessKeySecret: config["accessKeySecret"]}
	switch AccountInfo["isp"] {
	case "ali":
		p = &service.AliService{
			BaseConfig: *Baseconfig,
		}
	case "aws":
		p = &service.AwsService{
			BaseConfig: *Baseconfig,
		}
	}

	if err := p.GetInstances(); err != nil {
		return nil, errors.New("账号配置有误" + err.Error())
	}

	return p, nil
}
